package com.common.widget.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.common.R;
import com.common.utility.Logger;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class FormSelectView extends RelativeLayout {

    private String mTagString;
    private CharSequence[] mTextArray;//用于显示
    private CharSequence[] mTextValueArray;//用于id
    private Boolean mShowDivider;
    private String mPromptString;

    private LinearLayout mFormContentView;

    private TextView mTagTextView;
    private Spinner mSpinnerView;
    private View mDividerView;
    private boolean mHasDefaultValue = false;//spinner是否需要显示默认值，类似于请选择,或者""

    public FormSelectView(Context context) {
        super(context);
        init(null, 0);
    }

    public FormSelectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public FormSelectView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {

        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.FormView, defStyle, 0);

        mTagString = a.getString(
                R.styleable.FormView_form_tag);
        mTextArray = a.getTextArray(
                R.styleable.FormView_form_array);
        mTextValueArray = a.getTextArray(
                R.styleable.FormView_form_value_array);
        mShowDivider = a.getBoolean(
                R.styleable.FormView_form_divider, true);
        mPromptString = a.getString(
                R.styleable.FormView_form_prompt);
        a.recycle();

        LayoutInflater.from(getContext()).inflate(R.layout.view_form_select_view, this, true);
        mFormContentView = (LinearLayout) findViewById(R.id.form_content);
        mTagTextView = (TextView) findViewById(R.id.form_tag);
        mSpinnerView = (Spinner) findViewById(R.id.form_select);
        mDividerView = findViewById(R.id.form_divider);

        if (mPromptString != null) {
            mSpinnerView.setPrompt(mPromptString);
        } else {
            mSpinnerView.setPrompt("请选择");
        }

        if (mTagString != null) {
            mTagTextView.setText(mTagString);
        }

        mTagTextView.setTextColor(getResources().getColor(R.color.form_item_tag_text_color));
        //从xml中加载显示出来
        setArrayAdapter();

        mDividerView.setVisibility(mShowDivider ? View.VISIBLE : View.GONE);
        (findViewById(R.id.iv_arrow)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("mark","iv_arrow click");
                mSpinnerView.performClick();
            }
        });
    }

    public void setTag(String tag) {
        mTagTextView.setText(tag);
    }

    public String getText() {
        if (mSpinnerView != null && mSpinnerView.getSelectedItem() != null) {
            return mSpinnerView.getSelectedItem().toString();
        }
        return "";

    }

    public String getValue() {
        int pos = mSpinnerView.getSelectedItemPosition();
        if (mTextValueArray == null || mTextValueArray.length == 0) {
            return "";
        }
        return mTextValueArray[pos].toString();
    }

    /**
     * 通过返回的value值来进行选中
     * @param value 通常是唯一标识id
     */
    public void setSelectionByValue(String value) {
        Logger.e("form", "setValue() value:" + value);
        if (TextUtils.isEmpty(value) || mTextValueArray == null || mTextValueArray.length == 0) {
            return;
        }
        int pos = 0;
        for (int i = 0; i < mTextValueArray.length; i++) {
            if (value.equals(mTextValueArray[i])) {
                pos = i;
                break;
            }
        }
        mSpinnerView.setSelection(pos);
    }

    /**
     * 通过返回的显示名称来进行选中
     * @param name spinner中显示的名称
     */
    public void setSelectionByName(String name){
        Logger.e("form", "setName() name:" + name);
        if (TextUtils.isEmpty(name) || mTextArray == null || mTextArray.length == 0) {
            return;
        }
        int pos = 0;
        for (int i = 0; i < mTextArray.length; i++) {
            if (name.equals(mTextArray[i])) {
                pos = i;
                break;
            }
        }
        mSpinnerView.setSelection(pos);
    }



    private void setArrayAdapter(){
        if (mTextArray != null && mTextArray.length > 0) {
            ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(),
                    android.R.layout.simple_spinner_item,
                    mTextArray);
            adapter.setDropDownViewResource(R.layout.view_form_select_view_item);
            mSpinnerView.setAdapter(adapter);
        }
    }

    public void setAdapter(SpinnerAdapter adapter){
        mSpinnerView.setAdapter(adapter);
    }

    /**
     *
     * @param nameList
     * @param valueList
     * @param defaultName
     * @param defaultValue
     */
    public void setData(List<CharSequence> nameList,List<CharSequence> valueList,CharSequence defaultName,CharSequence defaultValue){
        nameList.add(0,defaultName);
        valueList.add(0,defaultValue);

        setData(nameList,valueList);
    }

    public void setData(List<CharSequence> nameList,List<CharSequence> valueList){
        if(nameList == null || nameList.isEmpty()){
            return;
        }

        if(valueList == null || valueList.isEmpty()){
            return;
        }

        if(nameList.size() != valueList.size()){
            return;
        }

        this.mTextArray = nameList.toArray(new CharSequence[]{});
        this.mTextValueArray = valueList.toArray(new CharSequence[]{});
        setArrayAdapter();
    }

    public void setData(List<JSONObject> jsonObjects,String key1,String key2){
        if(jsonObjects == null || jsonObjects.isEmpty()){
            return;
        }
        List<CharSequence> nameList = new ArrayList<>();
        List<CharSequence> valueList = new ArrayList<>();
        for (JSONObject jsonObject : jsonObjects){
            nameList.add(jsonObject.optString(key1));
            valueList.add(jsonObject.optString(key2));
        }
        setData(nameList,valueList);
    }

    public void setData(List<JSONObject> jsonObjects,String key1,String key2,CharSequence defaultName,CharSequence defaultValue){
        if(jsonObjects == null || jsonObjects.isEmpty()){
            return;
        }
        List<CharSequence> nameList = new ArrayList<>();
        List<CharSequence> valueList = new ArrayList<>();
        for (JSONObject jsonObject : jsonObjects){
            nameList.add(jsonObject.optString(key1));
            valueList.add(jsonObject.optString(key2));
        }

        setData(nameList,valueList,defaultName,defaultValue);
    }


}
